package org.edu.modules.personal.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.modules.personal.entity.VeBasePersonalSetting;
import org.edu.modules.personal.service.IVeBasePersonalSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"个人设置"})
@RestController
@RequestMapping({"/personal/veBase"})
@ApiSort(60)
public class VeBasePersonalController
{
    private static final Logger log = LoggerFactory.getLogger(VeBasePersonalController.class);
    @Autowired
    private IVeBasePersonalSettingService veBasePersonalSettingService;

    @AutoLog("通过userId查询个人设置信息")
    @ApiOperation(value="通过userId查询个人设置信息", notes="通过userId查询个人设置信息")
    @GetMapping({"/getPersonalSettingByUserId"})
    public Result<?> getPersonalSettingByUserId(@RequestParam(name="userId", required=true) String userId)
    {
        VeBasePersonalSetting veBasePersonalSetting = this.veBasePersonalSettingService.getPersonalSettingByUserId(userId);
        return Result.OK(veBasePersonalSetting);
    }

    @AutoLog("添加个人设置信息")
    @ApiOperation(value="添加个人设置信息", notes="添加个人设置信息")
    @PostMapping({"/addPersonalSetting"})
    public Result<?> addPersonalSetting(@RequestBody VeBasePersonalSetting veBasePersonalSetting)
    {
        if (veBasePersonalSetting.getUserId() == null) {
            return Result.error("用户id为空! ");
        }
        VeBasePersonalSetting model = this.veBasePersonalSettingService.getPersonalSettingByUserId(veBasePersonalSetting.getUserId());
        if (model != null) {
            return Result.error("该用户个人设置信息已存在! ");
        }
        this.veBasePersonalSettingService.save(veBasePersonalSetting);
        return Result.ok("添加成功!");
    }

    @AutoLog("编辑个人设置信息")
    @ApiOperation(value="编辑个人设置信息", notes="编辑个人设置信息")
    @PutMapping({"/editPersonalSetting"})
    public Result<?> editPersonalSetting(@RequestBody VeBasePersonalSetting veBasePersonalSetting)
    {
        this.veBasePersonalSettingService.updateById(veBasePersonalSetting);
        return Result.ok("修改成功!");
    }

    @AutoLog("个人设置信息批量删除")
    @ApiOperation(value="个人设置信息批量删除", notes="个人设置信息批量删除")
    @DeleteMapping({"/deletePersonalSettingBatch"})
    public Result<?> deletePersonalSettingBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veBasePersonalSettingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }
}
